import axios from 'axios'
import env from '@/config/env'
import {createSign} from '@/myutl/utlenc'

/**
 * [调用本地devServer进行签名操作]
 * @param  {[string-utf-8]} data [待签名数据]
 * @return {[jsonobj]}      [description]
 */
export const apiGetSign = async (data) => {
  // let buf = new Buffer(data);
  // let rsp = {};
  // try {
  //   rsp = await axios.get('/api/createsign?datab64='+buf.toString("base64"), {timeout: env.httptimeout});
  // }
  // catch(e) {
  //   console.warn('exp:', e);
  //   rsp.data = {};
  //   rsp.data.errno = -1;
  //   rsp.data.errmsg = "业务超时请重试-apiGetSign";
  //   rsp.data.sysmsg = e.code + ":" + e.message;
  //   console.warn("rsp.data:", rsp.data);
  // }
  // return rsp.data;

  let sign = createSign(data);
  console.log("apiGetSign:", sign);
  return {errno: 0, sign:sign};
}

/**
 * [调用后台用户登录接口]
 * @param  {[type]} params [description]
 * @return {[type]}      [description]
 */
export const apiUserLogin = async (params) => {
  let rsp = {};
  try {
    rsp = await axios.get("/user/userloginwithregister.do", 
      {params: params},{timeout: env.httptimeout});
  }
  catch(e) {
    console.warn('exp:', e);
    rsp.data = {};
    rsp.data.errno = -1;
    rsp.data.errmsg = "业务超时请重试-apiUserLogin";
    rsp.data.sysmsg = e.code + ":" + e.message;
    console.warn("rsp.data:", rsp.data);
  }
  return rsp.data;
}

/**
 * [调用后台用户登录密码修改接口]
 * @param  {[type]} params [description]
 * @return {[type]}      [description]
 */
export const apiModLoginPwd = async (params) => {
  let rsp = {};
  try {
    rsp = await axios.get("/user/userservice/changeloginpassword.do", 
      {params: params},{timeout: env.httptimeout});
  }
  catch(e) {
    console.warn('exp:', e);
    rsp.data = {};
    rsp.data.errno = -1;
    rsp.data.errmsg = "业务超时请重试-apiModLoginPwd";
    rsp.data.sysmsg = e.code + ":" + e.message;
    console.warn("rsp.data:", rsp.data);
  }
  return rsp.data;
}
